package com.sqx.modules.tbCoupon.controller.couponUser;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.sys.controller.AbstractController;
import com.sqx.modules.tbCoupon.entity.TbCouponUser;
import com.sqx.modules.tbCoupon.service.TbCouponUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@RestController
@Api(value = "用户端-优惠券", tags = {"用户端-优惠券"})
@RequestMapping(value = "/app/couponUser/")
public class AppTbCouponUserController extends AbstractController {

    @Autowired
    private TbCouponUserService couponUserService;


    @Login
    @ApiOperation("查看我的优惠券列表")
    @GetMapping(value = "getMyCouponList")
    public Result getMyCouponList(@ApiParam("用户id") @RequestAttribute Long userId,
                                  @ApiParam("页码")Integer page,
                                  @ApiParam("条数")Integer limit,
                                  @ApiParam("用户优惠券实体")TbCouponUser couponUser) {
        couponUser.setUserId(userId);
        return Result.success().put("data", couponUserService.getMyCouponList(page, limit, couponUser));
    }

    @Login
    @ApiOperation("获取订单可用优惠券")
    @GetMapping(value = "getMyOrderCouponList")
    public Result getMyOrderCouponList(@ApiParam("用户id")@RequestAttribute Long userId,
                                       @ApiParam("页码")Integer page,
                                       @ApiParam("条数")Integer limit,
                                       @ApiParam("优惠券可使用最低订单金额")BigDecimal orderMoney) {
        return Result.success().put("data", couponUserService.getMyOrderCouponList(page, limit,userId, orderMoney));
    }

    @Login
    @ApiOperation("领取活动优惠券")
    @GetMapping("receiveActivity")
    public Result receiveActivity(@ApiParam("用户id")@RequestAttribute("userId") Long userId, @ApiParam("优惠券id")Long couponId) {
        return couponUserService.receiveActivity(userId, couponId);
    }


    /**
     * 检测使优惠券过期
     */
    @Scheduled(cron = "0/2 * * * * ?", zone = "Asia/Shanghai")
    public void couponOverdue() {
        List<TbCouponUser> couponUserList = couponUserService.list(new QueryWrapper<TbCouponUser>().eq("status", 0).isNotNull("expiration_time"));
        for (TbCouponUser tbCouponUser : couponUserList) {
            if (tbCouponUser.getExpirationTime().before(new Date())) {
                tbCouponUser.setStatus(2);
                couponUserService.updateById(tbCouponUser);
            }
        }

    }
}